<?

$node = (int)param('node');

$_check = postdata('l01') == $DATA['page']['id'] + $node;

$_table = 'data';				// where info is stored
$_crit = "id_data=".D_MULTIBLOCK." AND id_parent in (".$DATA['page']['id'].", 0)";	// data selection criterium
$_order = 'id_index'; // ordered by this field
$_defaults = array('id_data' => D_MULTIBLOCK, 'id_parent' => $DATA['page']['id'], 'attr' => COM_ENABLED);
$_values = array('name', 'value');

$DATA['p'] = $p = array('edit' => permitted('edit', $node), 'configure' => permitted('configure', $node), );

$temp = $CURRENT->getPageById($node);	// !!! fix crumbs

$DATA['crumb'][] = array(
	'name' => $temp['localname'] ? $temp['localname'] : $temp['name'],
	'path' => _transformURL($CURRENT->url, array('remove_params' => 'all', 'add_param' => "node:$node")));
$DATA['crumb'] = array_mark_bounds($DATA['crumb']);



$DATA['components'] = array();
$DATA['cpn_list'] = array();
$DATA['group_list'] = array();

if (($temp = @glob($_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH.'*', GLOB_ONLYDIR)) && sizeof($temp)) {
	foreach ($temp as $t) {
		$_path = substr(strrchr($t, "/"), 1);
		if (strpos($_path, '[') !== false) continue;
		
		$_res = array("name" => $_path, "description" => "", "path" => $_path, "groupId" => "other", "group" => "Other");

		if (file_exists("$t/info.php")) {
			$t1 = include "$t/info.php";
			if ($t1 && $t1['name'])	$_res = array_merge($t1, array("path" => $_path));
		}
		
		$DATA['cpn_list'][$_path] = $DATA['components'][$_res['groupId']][] = $_res;
		$DATA['group_list'][$_res['groupId']] = $_res['group'];
	}

	$other = $DATA['components']['other'];
	unset($DATA['components']['other']);
	$DATA['components']['other'] = $other;
}

$DATA['objects'] = datafetch_array('db_list', array('table' => tb($_table), 'where' => $_crit, 'order' => $_order, 'index' => 'id' ));

foreach($DATA['objects'] as $id => $v) {
	$DATA['objects'][$id]['export'] = file_exists($_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH."$v[value]/admin/export/init.php");
	$DATA['objects'][$id]['import'] = file_exists($_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH."$v[value]/admin/import/init.php");
}


// tasks handling

if ($_check && $a = (int)param('delete')) {

	if ($DATA['component'] = datafetch('db_item', tb($_table), "$_crit AND (id=$a)")) {
		
		if (is_file($cl = $_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH."{$DATA[component][value]}/admin/cleanup/init.php")) {
			include $cl;
		} else {
			dataset('db_delete', tb($_table), "id_data in (".D_MB_SETTINGS.",".D_MB_DATA.",".D_MB_META.") AND (id_parent=$a)");
		}
		
		dataset('db_delete', tb($_table), "$_crit AND (id=$a)");
	}

//	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'delete')));
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('show')) {
	
	dataset('db_update', tb($_table), "attr = ".dbOr('attr', COM_VISIBLE), "$_crit AND (id=$a)");
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('hide')) {
	
	dataset('db_update', tb($_table), "attr = ".dbAnd('attr', ~COM_VISIBLE), "$_crit AND (id=$a)");
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('enable')) {
	
	dataset('db_update', tb($_table), "attr = ".dbOr('attr', COM_ENABLED), "$_crit AND (id=$a)");
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('disable')) {
	
	dataset('db_update', tb($_table), "attr = ".dbAnd('attr', ~COM_ENABLED), "$_crit AND (id=$a)");
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('globalize')) {
	
	dataset('db_update', tb($_table), "id_parent = 0", "$_crit AND (id=$a)");
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('localize')) {
	
	dataset('db_update', tb($_table), "id_parent = ".$DATA['page']['id'], "$_crit AND (id=$a)");
	exit;
}

if ($_check && $a = (int)param('stop') & COM_DEF_MASK) {
	$t = get_global('ui-adm-mb-f', COM_DEF_FILTER);
	set_global('ui-adm-mb-f', $t | $a);
	exit;
}

if ($_check && $a = (int)param('pass') & COM_DEF_MASK) {
	$t = get_global('ui-adm-mb-f', COM_DEF_FILTER);
	set_global('ui-adm-mb-f', $t & ~$a);
	exit;
}

if ($_check && $p['configure'] && $a = (int)param('tags')) {
	
	dataset('db_update', tb($_table), array('text' => trim(postdata(value))), "$_crit AND (id=$a)");
	exit;
}



if ($p['configure'] && $a = strtolower(trim(param('sort')))) {
	
	switch($a) {
	
		case 'manual':	// ajax request, no data should be returned

			$t = 1;
			
			parse_str(postdata('order'), $temp);
			
			if (is_array($temp = $temp['objects']) && sizeof($temp)) {

				foreach($temp as $id) {
					if ($id = (int)$id) {
						if ($DATA['objects'][$id]) {
							dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
							unset($DATA['objects'][$id]);
						}
					}
				}
				
				if (sizeof($DATA['objects']))
					foreach($DATA['objects'] as $id => $v) 
						dataset('db_update', tb($_table), "$_order = ".$t++, "$_crit AND id=$id");
			}
			
			exit;	// ajax: no more processing needed
			break;
}
	
	header("Location: "._transformURL($CURRENT->url, array('remove_param' => 'sort')));
	exit;
}




switch (action(array('create_object'))) {	// if POST contains submit button name

	case 'create_object':
		if ($p['configure']) {

			if ($name = trim(postdata('name'))) {
	//dbdebug();
				$max = ($m = datafetch('db_item', tb($_table), $_crit, "MAX($_order) as m")) ? $m['m']+1 : 1;
				
				foreach ($_values as $v)
					$_defaults[$v] = trim(postdata($v));
					
				$_defaults[$_order] = $max;
					
				if ($id = datafetch('db_insert', tb($_table), $_defaults)) {
	
					if (is_dir($_SERVER['DOCUMENT_ROOT'].FP_COMPONENT_PATH.$_defaults['value'].'/admin/settings'))	// if configurable - configure before edit
						header("Location: "._transformURL($CURRENT->url, array('add_param' => "settings:$id")));
					else
						header("Location: "._transformURL($CURRENT->url, array('add_param' => "component:$id")));
				
					exit;
				}
					// !!! else error on insert - handle it
			}
			
		}	// no permission given
		
		break;
	
}